package com.b.service.impl;

import com.b.domain.User;
import com.b.domain.Video;
import com.b.repository.UserRepository;
import com.b.repository.VideoRepository;
import com.b.repository.WatchHistoryRepository;
import com.b.vo.PageVo;
import com.b.vo.Result;
import org.checkerframework.checker.units.qual.A;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.SearchPage;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import com.b.dao.WatchHistoryMapper;
import com.b.domain.WatchHistory;
import com.b.service.WatchHistoryService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class WatchHistoryServiceImpl implements WatchHistoryService {

    @Resource
    private WatchHistoryMapper watchHistoryMapper;
    @Resource
    private WatchHistoryRepository watchHistoryRepository;
    @Resource
    private VideoRepository videoRepository;
    @Resource
    private UserRepository userRepository;

    @Override
    public int insert(WatchHistory record) {
        return watchHistoryMapper.insert(record);
    }

    @Override
    public int insertSelective(WatchHistory record) {
        return watchHistoryMapper.insertSelective(record);
    }

    @Override
    public List<WatchHistory> getAll() {
        return watchHistoryMapper.getAll();
    }


    @Override
    public int deleteByPrimaryKey(Integer id) {
        return watchHistoryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public WatchHistory selectByPrimaryKey(Integer id) {
        return watchHistoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(WatchHistory record) {
        return watchHistoryMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(WatchHistory record) {
        return watchHistoryMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result user_history_list(int userId, String title, Integer curPage, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(curPage-1, pageSize);
        System.out.println("userId = " + userId + ", title = " + title + ", curPage = " + curPage + ", pageSize = " + pageSize);
        Page<WatchHistory> histories = watchHistoryRepository.findWatchHistoriesByUserIdOrderByWatchTimeDesc(userId, pageRequest);
        PageVo<WatchHistory> watchHistoryPageVo  = new PageVo<>(curPage,(int) histories.getTotalElements(),pageSize);
        List<WatchHistory> watchHistoryList = new ArrayList<>();

        for (WatchHistory watchHistory : histories) {
            Integer videoId = watchHistory.getVideoId();
            Video video = videoRepository.findByIdAndTitleLike(videoId, title);
            if (video == null) {
                continue;
            }
            Integer userId1 = video.getUserId();
            User userById = userRepository.findUserById(userId1);
            video.setUser(userById);
            watchHistory.setVideo(video);
            watchHistoryList.add(watchHistory);
        }
        watchHistoryPageVo.setList(watchHistoryList);
        return new Result().setCode("200").setData(watchHistoryPageVo);

    }
}

